package com.sunny.l2;

import java.util.Stack;

public class Solution {

    public int evalRPN(String[] tokens) {

        Stack<Integer> stack = new Stack<>();

        for (String s: tokens){

            int tmp;
            try {
                tmp = Integer.parseInt(s);
            } catch (NumberFormatException e){
                //不是数字
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch (s){
                    case "+":
                        tmp = num1 + num2;
                        break;
                    case "-":
                        tmp = num1 - num2;
                        break;
                    case "*":
                        tmp = num1 * num2;
                        break;
                    case "/":
                        tmp = num1 / num2;
                        break;
                    default:
                        tmp = 0;
                }
            }
            //是数字
            stack.push(tmp);
        }
        return stack.pop();

    }

}
